package net.diaowen.dwsurvey.service.impl;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.alibaba.fastjson.JSON;
import net.diaowen.common.enums.DataStatusEnum;
import net.diaowen.common.plugs.page.Page;
import net.diaowen.common.service.BaseServiceImpl;
import net.diaowen.dwsurvey.config.DWSurveyConfig;
import net.diaowen.dwsurvey.dao.AnExcelFillblankDao;
import net.diaowen.dwsurvey.dao.AnFillblankDao;
import net.diaowen.dwsurvey.entity.AnExcelFillblank;
import net.diaowen.dwsurvey.entity.AnExcelFillblank;
import net.diaowen.dwsurvey.entity.Question;
import net.diaowen.dwsurvey.service.AnExcelFillblankManager;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * 填空题
 *
 * @author keyuan(keyuan258 @ gmail.com)
 * <p>
 * https://github.com/wkeyuan/DWSurvey
 * http://dwsurvey.net
 */
@Service
public class AnExcelFillblankManagerImpl extends BaseServiceImpl<AnExcelFillblank, String> implements AnExcelFillblankManager {

    @Autowired
    private AnExcelFillblankDao anExcelFillblankDao;

    @Override
    public void setBaseDao() {
        this.baseDao = anExcelFillblankDao;
    }

    @Override
    public void persistExcelData(String belongAnswerId, String surveyId, String quId, String filePath, String fileName) {
        try {
            String rootPath = DWSurveyConfig.DWSURVEY_WEB_FILE_PATH;
//            filePath = rootPath + filePath;
            filePath = rootPath + "webin/upload/ugx6njvsj/template.xlsx";
            FileInputStream fis = new FileInputStream(new File(filePath));
            ExcelReader reader = ExcelUtil.getReader(fis);
            List<List<Object>> readAll = reader.read();
            System.out.println(JSON.toJSONString(readAll));
            int index = 0;
            for (; index < readAll.size(); index++) {
                if (readAll.get(index).size() < 1) {
                    continue;
                }
                boolean headFlag = false;
                for (Object o :
                        readAll.get(index)) {
                    if (StringUtils.isNotBlank((String) o)) {
                        headFlag = true;
                        break;
                    }
                }
                if (headFlag) {
                    break;
                }
            }

            List<AnExcelFillblank> columnList = new ArrayList<>();
            for (index = index + 1; index < readAll.size(); index++) {

                List<Object> headList = readAll.get(index);
                for (int i = 0; i < headList.size(); i++) {
                    AnExcelFillblank anExcelFillblank = new AnExcelFillblank();
                    anExcelFillblank.setAnswer(String.valueOf(headList.get(i)));
                    anExcelFillblank.setRowId(String.valueOf(index+1));
                    anExcelFillblank.setColumnId(String.valueOf(i+1));
                    anExcelFillblank.setBelongAnswerId(belongAnswerId);
                    anExcelFillblank.setBelongId(surveyId);
                    anExcelFillblank.setQuId("888");
                    anExcelFillblank.setState(DataStatusEnum.VALID.getValue());
                    anExcelFillblank.setCreateTime(new Date());
                    anExcelFillblank.setUpdateTime(new Date());
                    columnList.add(anExcelFillblank);
                }
            }
            anExcelFillblankDao.saveAll(columnList);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }

}
